<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Edit_truck
 *
 * @author peerawatj20
 */
class edit_truck extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('truck_model');
        $this->load->model('catalog');
    }

    public function index() {
        $id = $this->input->get('id');
        $this->_set_form($id);
        $this->load->view('truck/edit_truck', $this->data);
    }

    private function _set_form($id) {
        $truck = $this->truck_model->get_truck_by_id($id);
        $this->data['hid_id'] = form_hidden('id', $id);
        $this->data['txt_license_plate'] = form_input('txtLicensePlate', $truck[0]->license_plate_code, 'class="form-control"');
        $this->data['cmb_brand'] = form_dropdown('cmbBrand', $this->catalog->list_truck_brand(), $truck[0]->brand_id, 'class="form-control" id="cmbBrand"');
        $this->data['cmb_model'] = form_dropdown('cmbModel', $this->catalog->list_truck_model($truck[0]->brand_id), $truck[0]->model_id, 'class="form-control" id="cmbModel"');
        $this->data['txt_moter_code'] = form_input('txtMoterCode', $truck[0]->moter_code, 'class="form-control"');
        $this->data['txt_mile_number'] = form_input('txtMileNumber', $truck[0]->mile_number, 'class="form-control"');
        $this->data['cmb_driver_name'] = form_dropdown('cmbDriverName', $this->truck_model->get_list_driver(), $truck[0]->driver_id, 'class="form-control"');
        $img = $this->truck_model->get_truck_img_by_id($id);
        $this->data['hid_count_img'] = form_hidden('count_img', count($img));
        
        for ($i = 0; $i < count($img); $i++) {
//             $this->data['img_picture'][$i] = '<img src="' . base_url() . 'uploads/trucks/' . $img[0]->file_name . '" style="width:280px" />';
            $this->data['img_picture'][$i] = '<div class="show-image">
                 <img src="' . base_url() . 'uploads/trucks/' . $img[$i]->file_name . '"  style="width:355px;" />
                 <input class="the-buttons" type="button" value="ลบ" id="btnDelete' . $i . '" onclick="' . $this->_js_delete_image($id, $img[$i]->file_number) . '" />
                 </div>';
        }
        $this->_set_error();
        
    }

    private function _js_delete_image($truck_id, $file_number) {
        $str = "if (confirm('ยืนยันการลบรายการ?') === true) {  location.href = 'edit_truck/delete_image?truck_id=" . $truck_id . "&file_num=" . $file_number . "'; }else{return false;}";
        return $str;
    }

    public function delete_image() {
        $id = $this->input->get('truck_id');
        $file_name = $this->input->get('file_num');
        
        $this->truck_model->delete_truck_image($id, $file_name);
        $img = $this->truck_model->get_truck_img_by_id($id);
        
        for ($i = 0; $i < count($img); $i++) {
            $this->truck_model->update_image_2($id, $i + 1, $img[$i]->file_name);
        }
        $this->_set_form($id);
        redirect('truck/edit_truck?id='.$id);
    }

    private function _set_error() {
        $this->data['err_license_plate'] = form_error('txtLicensePlate', '<div class="error">', '</div>');
        $this->data['err_brand'] = form_error('cmbBrand', '<div class="error">', '</div>');
        $this->data['err_model'] = form_error('cmbModel', '<div class="error">', '</div>');
    }

    public function validate() {
        $id = $this->input->post('id');

        $this->form_validation->set_rules('txtLicensePlate', 'License Plate', 'required');
        $this->form_validation->set_rules('cmbBrand', 'Brand', 'required');
        $this->form_validation->set_rules('cmbModel', 'Model', 'required');
        $this->form_validation->set_rules('txtMoterCode', 'Moter Code', '');
        $this->form_validation->set_rules('txtMileNumber', 'Mile Number', '');
        $this->form_validation->set_rules('cmbDriverName', 'Driver', '');

        $license_code = $this->input->post('txtLicensePlate');
        $brand_id = $this->input->post('cmbBrand');
        $model_id = $this->input->post('cmbModel');
        $moter_code = $this->input->post('txtMoterCode');
        $mile_number = $this->input->post('txtMileNumber');
        $driver_id = $this->input->post('cmbDriverName');

        $utility = new Config_form();

        $config['upload_path'] = './uploads/trucks/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '3072';
        $config['overwrite'] = FALSE;
        $config['encrypt_name'] = TRUE;

        $this->load->library('upload', $config);
        

        if ($this->form_validation->run() == FALSE) {
            $this->_set_form();
            $this->load->view('truck/edit_truck', $this->data);
        } else {
            $result = $this->truck_model->update_data($id, $license_code, $brand_id, $model_id, $moter_code, $mile_number, $driver_id);
            if ($result) {
                //ตัวแปรตั้งต้น
                $count_start = $this->input->post('count_img')<=0?0:$this->input->post('count_img');
                //ตัวแปรระบุจำนวน upload box
                $count_upload = $this->input->post('count_upload');
                //ลำดับไฟล์อัพโหลด ต่อ ไอดี
                $count = $this->input->post('count_img')<=0?1:$this->input->post('count_img')+1;
                for ($i = $count_start; $i < $count_upload; $i++) {
                    if ($_FILES['uploadFile' . $i]['name'] != NULL) {
                        if ($this->upload->do_upload('uploadFile' . $i)) {
                            $upload_data = $this->upload->data();
                            $this->truck_model->save_image($id, $count, $upload_data['file_name']);
                            $count++;
                        }
                    }
                }
                echo $utility->form_alert_success('บันทึกเสร็จเรียบร้อย', 'truck/home');
                exit();
            } else {
                $this->_set_form();
                $this->load->view('truck/edit_truck', $this->data);
            }
        }
    }

//put your code here
}
